﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Speedy
{


    [ProvideProperty("DisplayMember", typeof(Control))]
    [ProvideProperty("ValueMember", typeof(Control))]
    [ProvideProperty("DataSource", typeof(Control))]
    public class ComboBoxEditEx : DevExpress.XtraEditors.ComboBoxEdit
    {
        public ComboBoxEditEx()
        {
            this.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
        }

        private string m_valueMember;
        private string m_displayMember;

        [Category("Data")]
        [Description("Indicate the property to display for the items in this control.")]
        public string DisplayMember
        {
            get { return m_displayMember; }
            set { m_displayMember = value; }
        }

        [Category("Data")]
        [Description("Indicates the property to use as the actual value for the items in the control.")]
        public string ValueMember
        {
            get { return m_valueMember; }
            set { m_valueMember = value; }
        }

        [Category("Data")]
        [Description("Indicates the list that this control will use to get its items.")]
        public DataTable DataSource
        {
            set
            {
                ComboItem item = new ComboItem(this.ValueMember, this.DisplayMember);
                ComboBoxEditEx cmb = this;
                item.SetDataSource(ref cmb, value);
            }
        }

        [Browsable(false)]
        public object SelectedValue
        {
            get
            {
                ComboItem item = new ComboItem(ValueMember, DisplayMember);
                ComboBoxEditEx cmb = this;
                return item.GetItemValue(ref cmb);
            }
            set
            {
                ComboItem item = new ComboItem(this.ValueMember, this.DisplayMember);
                ComboBoxEditEx cmb = this;
                item.SelectItem(ref cmb, value);
            }
        }

        [Browsable(false)]
        public DevExpress.XtraEditors.Controls.ComboBoxItemCollection Items
        {
            get { return this.Properties.Items; }
        }

    }

    internal class ComboItem
    {

        private object m_value;
        private string m_displayText;

        internal ComboItem(object valueMember, string displayMember)
        {
            m_value = valueMember;
            m_displayText = displayMember;
        }

        public override string ToString()
        {
            return m_displayText;
        }

        internal object Value
        {
            get { return m_value; }
        }

        internal void SetDataSource(ref ComboBoxEditEx control, DataTable dt)
        {

            foreach (DataRow row in dt.Rows)
            {
                ComboItem item = new ComboItem(row[control.ValueMember], (string)row[control.DisplayMember]);
                control.Properties.Items.Add(item);

            }
        }

        internal object GetItemValue(ref ComboBoxEditEx control)
        {
            ComboItem item = control.SelectedItem as ComboItem;
            if (item == null)
            {
                return null;
            }
            else
            {
                return item.Value;
            }
        }

        internal void SelectItem(ref ComboBoxEditEx control, object value)
        {

            int index = 0;

            foreach (object obj in control.Properties.Items)
            {
                ComboItem item = obj as ComboItem;
                if (item != null && item.Value.Equals(value))
                {
                    break;
                }
                else
                {
                    index += 1;
                }
            }

            if (index >= control.Properties.Items.Count)
            {
                control.SelectedIndex = -1;
            }
            else
            {
                control.SelectedIndex = index;

            }
        }

    }

}
